------------------ fgui require start ------------------
------------------  fgui require end  ------------------
local GuildAddresSelectSkin = Class("GuildAddresSelectSkin");

function GuildAddresSelectSkin:ctor(com)
    ------------------ fgui事件绑定start ------------------
    self:bindChild(com);
    self.ddb_province.onClick:Add(self.onTouch_Ddb_province,self);
    self.ddb_province.onChanged:Add(self.onChanged_Ddb_province,self);
    self.ddb_city.onClick:Add(self.onTouch_Ddb_city,self);
    self.ddb_city.onChanged:Add(self.onChanged_Ddb_city,self);
    self:init();
    ------------------  fgui事件绑定end  ------------------
end

function GuildAddresSelectSkin:bindChild(com)
    ------------------ fgui变量绑定start ------------------
    self.fguiObj = com;
    self.text_Guild = com:GetChild("Text_Guild");
    self.text_province = com:GetChild("Text_province");
    self.ddb_province = com:GetChild("Ddb_province");
    self.ddb_city = com:GetChild("Ddb_city");
    ------------------  fgui变量绑定end  ------------------
end

function GuildAddresSelectSkin:getProvince()
    return self.ddb_province.text;
end

function GuildAddresSelectSkin:getCity()
    return self.ddb_city.text;
end

function GuildAddresSelectSkin:init()
    self.text_Guild.text = getLanguage("GuildInvestment_1044");
    self.text_province.text = getLanguage("GuildInvestment_1045");
    self.provinceIdx = 1;
    self.cityIdx = 1;
    self:updateView();
    self.ddb_province.visibleItemCount = 4;
    self.ddb_province.items = logicMgr.GuildManager.getProvinces();
    self.ddb_province.selectedIndex = 0;
    self.ddb_province_dropItemControll = nil;

    self.ddb_city.visibleItemCount = 4;
    self.ddb_city.items = logicMgr.GuildManager.getProvinceCitys(self.provinceIdx);
    self.ddb_city.selectedIndex = 0;
    self.ddb_city_dropItemControll = nil;
end


function GuildAddresSelectSkin:updateView()
    local unionSetting = logicMgr.GuildManager.getUnionSetting();
    for k , v in pairs(logicMgr.GuildManager.db_guildlocation) do
        if v.f_GuildProvince == unionSetting.province then
            self.provinceIdx = k;
            for k2 , v2 in pairs(v.f_GuildCity) do
                if v2 == unionSetting.city then
                    self.cityIdx = k2;
                end
            end
            break
        end
    end
    self.ddb_province.text = logicMgr.GuildManager.getProvince(self.provinceIdx);
    self.ddb_city.text = logicMgr.GuildManager.getProvinceCity(self.provinceIdx,self.cityIdx);
end

function GuildAddresSelectSkin:updateCitySelected()
    local selController = self.ddb_city.dropdown:GetChild("list"):GetChildAt(self.ddb_city.selectedIndex):GetController("Ctl_seled");
    selController.selectedIndex = 1;
    if self.ddb_city_dropItemControll then
        self.ddb_city_dropItemControll.selectedIndex = 0;
    end
    self.ddb_city_dropItemControll = selController;
    self.cityIdx = self.ddb_city.selectedIndex + 1;
end

function GuildAddresSelectSkin:updateProvinceSelected()
    local selController = self.ddb_province.dropdown:GetChild("list"):GetChildAt(self.ddb_province.selectedIndex):GetController("Ctl_seled");
    selController.selectedIndex = 1;
    if self.ddb_province_dropItemControll then
        self.ddb_province_dropItemControll.selectedIndex = 0;
    end
    self.ddb_province_dropItemControll = selController;
    if self.provinceIdx ~= self.ddb_province.selectedIndex + 1 then
        self.provinceIdx = self.ddb_province.selectedIndex + 1;
        self.cityIdx = 1;
        self.ddb_city.items = logicMgr.GuildManager.getProvinceCitys(self.provinceIdx);
        self.ddb_city.text = logicMgr.GuildManager.getProvinceCity(self.provinceIdx,self.cityIdx);
        self.ddb_city.selectedIndex = 0;
        if self.ddb_city_dropItemControll then
            self.ddb_city_dropItemControll.selectedIndex = 0;
        end
        self.ddb_city_dropItemControll = nil;
    end
end

function GuildAddresSelectSkin:OnDestroy()
    ------------------ fgui事件移除start ------------------
    self:beforeDestroy();
    self.ddb_province.onClick:Clear();
    self.ddb_province.onChanged:Clear();
    self.ddb_city.onClick:Clear();
    self.ddb_city.onChanged:Clear();
    ------------------  fgui事件移除end  ------------------
end

function GuildAddresSelectSkin:onChanged_Ddb_province(event)
    self:updateProvinceSelected();
end

function GuildAddresSelectSkin:onTouch_Ddb_province(obj)
    if self.ddb_province_dropItemControll == nil then
        self:updateProvinceSelected();
    end
end

function GuildAddresSelectSkin:onChanged_Ddb_city(event)
    self:updateCitySelected();
end

function GuildAddresSelectSkin:onTouch_Ddb_city(obj)
    if self.ddb_city_dropItemControll == nil then
        self:updateCitySelected();
    end
end

------------------  ↑ fgui事件句柄 ↑  ------------------

function GuildAddresSelectSkin:beforeDestroy() end

return GuildAddresSelectSkin;
